<template>
  <div>
    <home-header v-if="this.$store.state.search"></home-header>
    <div class="icons">
      <swiper :options="swiperOptions" v-if="showSwiper">
        <swiper-slide v-for="(item, index) of food" :key="item.food_id">
          <div
            class="icon"
            v-for="item of food"
            :key="item.food_id"
          >
            <router-link :to="{name: '/Fooddetail', params:{id:item.food_id}}">
            <div class="icon-img">
              <img
                class="icon-img-content"
                :src="item.food_picture"
              >
            </div>
              <p class="content">{{item.food_content}}</p>
              <p class="icon-desc">【{{item.food_title}}】</p>
            </router-link>
          </div>
        </swiper-slide>
      </swiper>
    </div>
    <img v-if="btnFlag" class="go-top" src="../../../assets/images/top.png"  @click="backTop">
  </div>
</template>

<script>
    import HomeHeader from '../../home/components/Header'
    import axios from 'axios'
    export default {
        name: "FoodFoods",
        props:['food'],
        components: {
            HomeHeader
        },
        data () {
            return {
                food_id: '',
                food_picture: '',
                food_title:'',
                food_content: '',
                btnFlag:false,
                scrollTop:2,
                moveOffset:0,
                swiperOptions: {
                    pagination: '.swiper-pagination',
                    loop: true
                }
            }
        },
        mounted(){
            // axios.get('/api/foods/index.html')
            //     .then((res) => {
            //         console.log(res);
            //         this.food = res.data.data.data;
            //     })
            //     .catch((err) => {
            //         console.log(err);
            //     });
            window.addEventListener('scroll', this.scrollToTop)
        },
        destroyed(){
            window.removeEventListener('scroll', this.scrollToTop)
        },
        computed: {
            //轮播icons
            pages () {
                const pages = []
                this.iconList.forEach((item,index) =>{
                    const page = Math.floor(index / 8)
                    if (!pages[page]) {
                        pages[page] = []
                    }
                    pages[page].push(item)
                })
                return pages
            },
            showSwiper () {
                return this.food.length//解决了自动滚动效果
            }
        },
        methods: {
            // 点击图片回到顶部方法，加计时器是为了过渡顺滑
            backTop () {
                const that = this;
                let timer = setInterval(() => {
                    let ispeed = Math.floor(-that.scrollTop / 5)
                    document.documentElement.scrollTop = document.body.scrollTop = that.scrollTop + ispeed
                    if (that.scrollTop === 0) {
                        clearInterval(timer)
                    }
                }, 16)
            },
            // 为了计算距离顶部的高度，当高度大于60显示回顶部图标，小于60则隐藏
            scrollToTop () {
                const that = this;
                // 页面滚动距顶部距离
                let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
                var scroll = scrollTop - that.moveOffset;
                that.moveOffset = scrollTop;
                that.scrollTop = scrollTop
                if (scroll > 0) {
                    // 向上滑动
                    that.btnFlag = true
                } else {
                    // 向下滑动
                    that.btnFlag = false
                }
            }
        }
    }
</script>

<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  @import '~styles/mixins.styl'
  .end
    text-align: center;
    margin-top: 25px;
    margin-bottom: 30px;
  .go-top
    width: 0.96rem ;
    height: 0.96rem;
    position: fixed;
    bottom: 1.11rem;
    right: 0.67rem;

  .title
    margin-top .2rem
    line-height .8rem
    background #eee
    text-indent .2rem
  .icons
    margin-top .2rem
    .icon
      position: relative;
      overflow: hidden;
      width: 45%;
      right: 0;
      height: 0;
      float: left;
      padding-bottom: 70%;
      margin-top: 10px;
      margin-left: 12px;
      background: #eee;
      border-radius: 35px;
      .icon-img
        position absolute
        top 0
        left 0
        right 0
        bottom 50px
        box-sizing border-box
        padding 5px
        .icon-img-content
          display block
          margin 0 auto
          height 100%
      .icon-desc
        position absolute
        left 0
        right 0
        bottom 0
        height 22px
        line-height 22px
        text-align center
        color $darkTextColor
        ellipsis()
      .content
        margin-top: 216px;
        margin-left: 6px;
        ellipsis()
</style>
